package com.suxg.hbase.auto;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class ScanCells {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println((new Byte[123]));
	}

	private static Connection connection;

	static {
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
		// 如果是集群 则主机名用逗号分隔
		configuration.set("hbase.zookeeper.quorum", "localhost:2181");
		configuration.set("hbase.nodeParent", "localhost:2181");

		try {
			connection = ConnectionFactory.createConnection(configuration);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param <T>
	 * @param tableName
	 * @param mapper
	 * @return
	 */
	public static <T> List<T> getScanCellList(String tableName, String rowKey) {
		List list = new ArrayList<>();
		Table table = null;
		List<T> rs = new ArrayList<T>();
		ResultScanner scanner = null;
		try {
			table = connection.getTable(TableName.valueOf(tableName));

			Scan scan = new Scan();
			// 进行全表扫描加开始，结束行，优化查询效率
//            scan.setStartRow(null);
//            scan.setStopRow(null);

			Get get = new Get(Bytes.toBytes(rowKey));
			Result resultCell = table.get(get);
			// result.listCells();//可以考虑
			Map<byte[], byte[]> familyMap = resultCell.getFamilyMap(Bytes.toBytes("student"));
			for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {

				System.out.println(Bytes.toString(entry.getKey()));
				list.add(entry.getKey());
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			scanner.close();
			try {
				connection.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return list;

	}
}
